﻿using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseServices.DalEf;

namespace EnterpriseServices.Service
{
    /// <summary>
    /// 
    /// </summary>
    public class ValidationServiceJSON : ValidationServiceBase, IValidationManagerJSON
    {
        #region IValidationManagerJSON Members

        /// <summary>
        /// Returns true if the userName exists.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public bool DoesUserExist(string userName)
        {
            try
            {
                return base.DoesUserExist(userName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if the userName exists.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public bool DoesUserExistPass(string userName, string password)
        {
            try
            {
                return base.DoesUserExistPass(userName, password);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if a userName belongs to the requested role.
        /// A userName belongs to a role if
        /// <list type="bullet">
        /// 		<item>the userName is directly attached to a role</item>
        /// 		<item>the groups the userName belongs to are attached to a role</item>
        /// 		<item>the groups' child groups are attached to a role</item>
        /// 		<item>the associated groups the userName belongs to are attached to a role</item>
        /// 	</list>
        /// Regarding Groups, a userName only belongs to a role if he is eiher a group owner or a group member.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public bool IsUserInRole(string applicationName, string roleName, string userName)
        {
            try
            {
                return base.IsUserInRole(applicationName, roleName, userName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if a userName belongs to the requested role.
        /// A userName belongs to a role if
        /// <list type="bullet">
        /// 		<item>the userName is directly attached to a role</item>
        /// 		<item>the groups the userName belongs to are attached to a role</item>
        /// 		<item>the groups' child groups are attached to a role</item>
        /// 		<item>the associated groups the userName belongs to are attached to a role</item>
        /// 	</list>
        /// Regarding Groups, a userName only belongs to a role if he is eiher a group owner or a group member.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public bool IsUserInRolePass(string applicationName, string roleName, string userName, string password)
        {
            try
            {
                return base.IsUserInRolePass(applicationName, roleName, userName, password);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Determines whether [is group in role] [the specified application name].
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="groupName">Name of the group.</param>
        /// <returns>
        /// 	<c>true</c> if [is group in role] [the specified application name]; otherwise, <c>false</c>.
        /// </returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public bool IsGroupInRole(string applicationName, string roleName, string groupName)
        {
            try
            {
                return base.IsGroupInRole(applicationName, roleName, groupName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

         /// <summary>
        /// Gets the name of the userNames by application.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <returns></returns>
        [TransactionFlow(TransactionFlowOption.Allowed)]
        [OperationBehavior(TransactionScopeRequired = false)]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        new public string[] GetUsersByApplicationName(string applicationName)
        {
            try
            {
                  List<User> myUserList = base.GetUsersByApplicationName(applicationName); 
                  int myCount = myUserList.Count;
                  int i = 0;
                  String[] myStringList = new String[myCount];  
                  
                    myUserList.ForEach(delegate(User ur)
                  {
                       myStringList[i] = ur.UserName.ToString();
                       i += 1;
                  });
                  return myStringList;
            }
           catch (Exception ex)
           {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return null;
           }
        }     

        #endregion
    }
}
